// ===== 引入工具类
const util = require( "./util");
const dbUtil = require( "./dbUtil");
const apiUtil = require( "./apiUtil");

module.exports = {
  /**
   * 判断订单是否保存过
   * @param {*} context 
   * @param {*} oid 
   */
  async checkC_user_top_goods_order_logExist(context,oid){
    const count = await dbUtil.selectCount(context,
                                    "c_user_top_goods_order_log",
                                    {oid});
    return count>0;
  },
  /**
   * 每次购买增加的翻卡次数+3	11.1-11.3  次数+6
   */
  async getLotteryNumberForEveryBoughtTopGoods(){
    if(await util.isBetweenNowTime("2020-11-01 00:00:00","2020-11-03 23:59:59")){
        return 6;
    }
    return 3;
  },
  /**
   * 获取签到打卡应该送的积分数
   */
  async getAward_valueForSign(context,user_sign_count){
    let sign_award_rule = await dbUtil.selectListAll(context,'c_sign_award_rule',{},{});
    //默认10个积分
    let award_value = 10;
    for (let item of sign_award_rule) {
        if (item.days == user_sign_count) {
            if (item.type == 1) {// 1:酷币类型
                return item.award_value;
            }
        }
    }
    return award_value;
  },
  /**
   * 6-50元优惠券（满51元可用）
   * 限量100份*3  
   * 各在3个时间段解锁100份（10.28-10.31 / 11.1-11.3 / 11.4-11.9）
   * @param {*} context 
   * @param {*} award_reversal_list 
   */
  async filterForType6Award(context,award_reversal_list){
      let award_reversal_list_new = [];
          
      //50元优惠券的当前中奖数量
      let count = await dbUtil.selectCount(context,"c_award_issue",{award_type:5,award_source:2})
      for(let award_reversal of award_reversal_list){
        if(award_reversal.type==6){
            // 判断用户是否兑换过优惠券  -------- start
            let c_award_issue_count = await dbUtil.selectCount(context,'c_award_issue',{open_id:context.openId,award_source:award_reversal.source,award_type:6});
            if (c_award_issue_count >= 1) {
                //抽过50元优惠券的就不能再抽了
                continue;
            }
            let limitCount = 0;
            if(await util.isBetweenNowTime('2020-10-01 00:00:00','2020-10-31 23:59:59')){
                limitCount=100;
            }else if(await util.isBetweenNowTime('2020-11-01 00:00:00','2020-11-03 23:59:59')){
                limitCount=200;
            }else if(await util.isBetweenNowTime('2020-10-04 00:00:00','2020-11-10 00:00:00')){
                limitCount=300;
            }
            
            if(count<limitCount){
                award_reversal_list_new.push(award_reversal);
            }

        }else{
            award_reversal_list_new.push(award_reversal);
        }
      }
      return award_reversal_list_new;
  },
  /**
   * 检查奖项当天的兑换数量是否还有剩余
   * @param {*} context 
   * @param {*} currency_convert 当前奖品
   * @return 返回true标识还有抽奖数量，false标识当前抽奖数量已经上限
   */
  async checkCurrencyConvertDayLimit(context,currency_convert){
    let time_start = await util.getTimeByDay(0);
    let time_end = await util.getTimeByDay(1);
    let param1 = {
        award_source:currency_convert.source,
        award_type:currency_convert.type,
        award_time:{$gte:time_start,$lt:time_end}
    }
    //该奖品今日的数量限制
    let limitNum = await this.getCurrencyConvertDayLimit(currency_convert);
    // 奖品当天的兑换数量
    let todayCount = await dbUtil.selectCount(context,'c_award_issue',param1);
    
    return todayCount<limitNum;
  },
  /**
   * 获取该奖项今日限制的数量
   * @param {*} currency_convert 
   */
  async getCurrencyConvertDayLimit(currency_convert){
    // 奖品当天的兑换数量
    if(currency_convert.type==1){
        //1	10元优惠券（满11元可用）	333	每日限量760份 / 共9880份
        return 760;
        // return 2;
    }else if(currency_convert.type==2){
        //2	20元优惠券（满21元可用）	666	每日限量380份 / 共4940份
        return 380;
        // return 2;
    }else if(currency_convert.type==3){
        //3	30元优惠券（满31元可用）	999	每日限量76份 / 共988份
        return 76;
    }else if(currency_convert.type==4){
        //4	999元行李箱	11111	限量3份（指定日期11月9日兑换）
        if(await util.isBetweenNowTime("2020-11-09 00:00:00","2020-11-09 23:59:59")){ 
        // if(await util.isBetweenNowTime("2020-10-28 00:00:00","2020-10-28 23:59:59")){
            return 3;
        }else{
            return 0;
        }
    }else if(currency_convert.type==5 
            || currency_convert.type==6 
            ||currency_convert.type==7){
        //5	399元钱包礼盒	3333	每日限量11-22份/共200份
        //6	299元腰包	2222	每日限量11-22份/共200份
        //7	199元墨镜	1111	每日限量11-22份/共200份
        let limitNum = 11;
        if(await util.isBetweenNowTime("2020-11-01 00:00:00","2020-11-03 23:59:59")){
            return 22;
        }
        if(await util.isBetweenNowTime("2020-11-04 00:00:00","2020-11-09 23:59:59")){
            return 15;
        }
        return limitNum;
    }
    return 0;
  },
  /**
   * 获取当前登录的用户
   */
  async getLoginUser(context){
      let user = await dbUtil.selectOne(context,'c_user',{open_id:context.openId});
      return user;
  },
  /**
   * 记录分值变化明细
   * @param {*} context 
   * @param {*} open_id 
   * @param {*} text 
   * @param {*} gameCurrency 
   */
  async saveGame_currency_log(context,text,gameCurrency){
    let user = await this.getLoginUser(context);
    let {nick_name,open_id} = user;
    let createTime =await util.getNowTime();
    let game_currency_log = {nick_name,open_id,text,gameCurrency,createTime};
    await dbUtil.insertOne(context,"game_currency_log",game_currency_log)
  },
  /**
   * 保存翻牌日志
   * @param {*} context 
   * @param {*} open_id 
   * @param {*} nick_name 
   * @param {*} award_reversal 
   */
  async saveAward_reversal_log(context,open_id,nick_name,award_reversal){
    let createTime =await util.getNowTime();
    let log = {
        open_id,nick_name,createTime,
        result:award_reversal?"中奖":"未中奖",
    };
    if(award_reversal){
        log.type=award_reversal.type;
        log.source=award_reversal.source;
        log.award_name=award_reversal.award_name;
        log.award_value=award_reversal.award_value;
    }
    await dbUtil.insertOne(context,"c_award_reversal_log",log);
  }
}